/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.scheduling;

import org.openislands.oi.constant.RoleType;
import org.openislands.oi.constant.MessageCodeEnum;
import org.openislands.oi.pojo.dto.CommonResult;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.*;

public class CoordinationResult<E> implements Serializable {
    private MessageCodeEnum code;
    private String message;
    private String errorMessage;
    private Map<RoleType, List<NodeCoordinationResult<E>>> nodesResult;

    @Resource
    private StateEngine stateEngine;

    public CoordinationResult() {
        this.nodesResult = new HashMap<>();
    }

    public void addOneOfResult(RoleType role, String nodeId, CommonResult<E> nodeResult) {
        if (!this.nodesResult.containsKey(role)) {
            this.nodesResult.put(role, new ArrayList<>());
        }
        this.nodesResult.get(role).add(new NodeCoordinationResult<>(nodeId, nodeResult));
    }

    public MessageCodeEnum calculateCoordinationResult() {
        Set<MessageCodeEnum> nodesResultCode = new HashSet<>();
        List<String> errorMessages = new ArrayList<>();
        this.nodesResult.forEach((role, results) -> {
            results.forEach((nodeResult) -> {
                nodesResultCode.add(nodeResult.getResult().getCode());
                if (!nodeResult.getResult().isSuccess()){
                    errorMessages.add(String.format("%s %s failed with message: %s", role, nodeResult.getNodeId(), nodeResult.getResult().getMessage()));
                }
            });
        });
        boolean hasSuccess = nodesResultCode.contains(MessageCodeEnum.SUCCESS);
        boolean hasNotEffective = nodesResultCode.contains(MessageCodeEnum.NOT_EFFECTIVE);
        if (!errorMessages.isEmpty()){
            this.message = String.join("\n", errorMessages);
        }
        if (nodesResultCode.size() == 1) {
            this.code = nodesResultCode.iterator().next();
        } else if (nodesResultCode.size() == 2 && hasSuccess && hasNotEffective) {
            this.code = MessageCodeEnum.NOT_EFFECTIVE;
        } else {
            this.code = MessageCodeEnum.DAG_SCHEDULING_ERROR;
        }
        return this.code;
    }

    public Boolean isSuccess() {
        return this.code.isSuccess();
    }

    public void setCode(MessageCodeEnum code) {
        this.code = code;
    }

    public MessageCodeEnum getCode() {
        return this.code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public Map<RoleType, List<NodeCoordinationResult<E>>> getNodesResult() {
        return nodesResult;
    }

    public void setNodesResult(Map<RoleType, List<NodeCoordinationResult<E>>> nodesResult) {
        this.nodesResult = nodesResult;
    }
}
